package com.demo2do.fms.admin.web.controller.supplier;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Preloading;
import com.demo2do.fms.admin.service.supplier.TrailerService;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.entity.vehicle.trailer.Trailer;

/**
 *
 * @author Ark
 *
 */
@Controller
@RequestMapping("/trailers")
@MenuSetting("menu-supplier")
public class TrailerProfileEditController {
    
    private static final Logger logger = LoggerFactory.getLogger(TrailerProfileEditController.class);
    
    @Autowired
    private TrailerService trailerService;
    
    /**
     * 挂车详情
     * 
     * @param trailerId
     * @param trailerMenuType
     * @return
     */
    @RequestMapping(value = "/{trailerId}", method = RequestMethod.GET)
    @MenuSetting("submenu-{trailerMenuType}-trailer-management")
    public ModelAndView view(@PathVariable("trailerId") Long trailerId,
                             @RequestParam("trailerMenuType") String trailerMenuType) {

        ModelAndView modelAndView = new ModelAndView("supplier/management/trailer/profile/trailer-profile-view");
        
        Trailer trailer = trailerService.load(Trailer.class, trailerId);
        modelAndView.addObject("trailer", trailer);
        
        modelAndView.addObject("trailerMenuType", trailerMenuType);

        return modelAndView;
        
    }
    
    /**
     * 
     * @param vehicleId
     * @return
     */
    @RequestMapping(value = "/{id}/edit-info", method = RequestMethod.GET)
    public ModelAndView editInfo(@PathVariable("id") Long id) {

        ModelAndView modelAndView = new ModelAndView("supplier/management/trailer/profile/trailer-info-edit");

        Trailer trailer = trailerService.load(Trailer.class, id);
        
        modelAndView.addObject("trailer", trailer);
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param trailer
     * @param operator
     * @return
     */
    @RequestMapping(value = "/{id}/edit-info", method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEditInfo(@Preloading("id") Trailer trailer,
                                               @Secure(property = "id") Long operator) {

        HttpResult httpResult = new HttpResult();
        
        try {

            trailerService.edit(trailer, operator);
            
            httpResult.success().message("修改成功");
            
        } catch (Exception e) {
            logger.error("#onEditInfo() - edit fail, exception = [{}]", e);
            httpResult.fail("500").message("修改成功");
        }
        
        return httpResult;
        
    }
    
    /**
     * 更新图片panel
     * 
     * @param id
     * @param type
     * @param fileObject
     * @param objectId
     * @param modal
     * @param acceptType
     * @return
     */
    @RequestMapping(value = "/{id}/cover", method = RequestMethod.GET)
    public ModelAndView editCover(@PathVariable("id") Long id,
                                  @RequestParam("type") String type,
                                  @RequestParam("fileObject") String fileObject,
                                  @RequestParam("objectId") Long objectId,
                                  @RequestParam(name = "modal", required = false, defaultValue = "false") boolean modal,
                                  @RequestParam(name = "acceptType", required = false) String acceptType){
        
        ModelAndView modelAndView = new ModelAndView("supplier/management/trailer/profile/trailer-cover-edit");
        
        modelAndView.addObject("id", id);
        modelAndView.addObject("type", type);
        modelAndView.addObject("fileObject", fileObject);
        modelAndView.addObject("objectId", objectId);
        modelAndView.addObject("modal", modal);
        modelAndView.addObject("acceptType", acceptType);
        
        return modelAndView;
        
    }
    
}